<?php

namespace Admin\Controller;

use JohnLui\AliyunOSS;
use lyf\Str;
use Think\Controller;

require './vendor/autoload.php';
header('content-type:text/plain;charset=utf-8');

class LoginController extends Controller
{
    public function index()
    {
        $this->display();
    }

    public function agent()
    {
        $this->display();
    }

    /**
     * @desc 加入验证码
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     */
    public function verify()
    {
        $config = array(
            'fontSize' => 30,    // 验证码字体大小
            'length' => 4,     // 验证码位数
            'useNoise' => true, // 关闭验证码杂点
            'useZh' => false, // 开启中文验证码
            'codeSet' => '0123456789', // 开启中文验证码
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }

    //发送手机验证码

    public function phone()
    {
        $username = I('username');
        $pwd = I('password');
        if ($username == "" or $pwd == "") {
            $this->error('用户名和密码不能为空！');
            return false;
        }
        $m = M('admin');
        $where['username|phone'] = array('eq', $username);
        $where['password'] = array('eq', md5($pwd));
        $res = $m->where($where)->find();
        if (!$res) {
            $this->error('用户名或密码错误！');
            return false;
        }
//        $ret = $m->where($where)->find();


        $code = rand(100000, 999999);
        $phone = $res['phone'];
        $type = "0";
//        var_dump($code);
        sendAliSms($phone, $type, $code);
        $date['uid']=$res['id'];
        $date['phone']=$res['phone'];
        $date['status']=1;
        $date['type']=3;
        $date['code']=$code;
        $date['create_time']=time();
        $date['content']="发送成功";
        M('sms')->add($date);
    }

    //登录
    public function login()
    {
        if (IS_POST) {
            if (!IS_AJAX) {
                $this->error('提交方式不正确！');
            } else {
                $username = I('username');
                $password = md5(I('password'));
                $code = I('verify');

                if (empty($username) || empty($password) || empty($code)) {
                    $this->error('请填写完整的信息');
                    return false;
                }

				$verify = new \Think\Verify();
				$is_verify=  $verify->check($code, '');

				if (!$is_verify){
					$this->error('验证码错误，请重试！');
				}


                $map['username|email']=$username;
				$map['password']=$password;
                $is_exist = M('admin')->where($map)->find();
                if (!$is_exist){
                    $this->error('用户名或密码错误！');
                    return false;
                }

				session(array('name' => 'adminster', 'expire' => 3600 * 24 * 3));
				session('adminster', $is_exist);


				$save_data['last_ip'] = get_client_ip();
				$save_data['last_time'] = time();
				M('admin')->where(['id'=>$is_exist['id']])->save($save_data);
				$this->success('登录成功,跳转中~', U('Admin/Index/index'), 1);
            }
        }
    }

    public function logout()
    {
        session('adminster', null);
        $this->redirect('Admin/Login/index');
    }

}

?>